package cn.exrick.xboot.modules.your.controller;

import cn.exrick.xboot.common.utils.PageUtil;
import cn.exrick.xboot.common.utils.ResultUtil;
import cn.exrick.xboot.common.vo.PageVo;
import cn.exrick.xboot.common.vo.Result;
import cn.exrick.xboot.modules.your.entity.Bed;
import cn.exrick.xboot.modules.your.service.IBedService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author chenyunxuan
 */
@Slf4j
@RestController
@Api(description = "陪护床管理接口")
@RequestMapping("/xboot/bed")
@Transactional
public class BedController {

    @Autowired
    private IBedService iBedService;

    @RequestMapping(value = "/get/{id}", method = RequestMethod.GET)
    @ApiOperation(value = "通过id获取")
    public Result<Bed> get(@PathVariable String id) {

        Bed bed = iBedService.getById(id);
        return new ResultUtil<Bed>().setData(bed);
    }

    @RequestMapping(value = "/getAll", method = RequestMethod.GET)
    @ApiOperation(value = "获取全部数据")
    public Result<List<Bed>> getAll() {

        List<Bed> list = iBedService.list();
        return new ResultUtil<List<Bed>>().setData(list);
    }

    @RequestMapping(value = "/getByPage", method = RequestMethod.GET)
    @ApiOperation(value = "分页获取")
    public Result<IPage<Bed>> getByPage(@ModelAttribute PageVo page, @ModelAttribute Bed bed) {
        QueryWrapper<Bed> qw = new QueryWrapper<>();
        if (bed.getHospitalId() != null) {
            if (bed.getHospitalId().equals("1")) {
                qw.eq("a.hospital_id", "");
            } else {
                qw.eq("a.hospital_id", bed.getHospitalId());
            }
        } else {
            qw.ne("a.hospital_id", "");
        }
        if (bed.getBedElectricity() != null) {
            qw.lt("a.bed_electricity", 20);
        }
        if (bed.getBedCode() != null) {
            qw.like("a.bed_code", bed.getBedCode());
        }
        if (bed.getFaultFlag() != null) {
            qw.like("a.fault_flag", bed.getFaultFlag());
        }
        qw.eq("a.delete_flag", 0);
        IPage<Bed> data = iBedService.pageBed(PageUtil.initMpPage(page), qw);
        return new ResultUtil<IPage<Bed>>().setData(data);
    }

    @RequestMapping(value = "/insertOrUpdate", method = RequestMethod.POST)
    @ApiOperation(value = "编辑或更新数据")
    public Result<Bed> saveOrUpdate(@ModelAttribute Bed bed) {

        if (iBedService.saveOrUpdate(bed)) {
            return new ResultUtil<Bed>().setData(bed);
        }
        return new ResultUtil<Bed>().setErrorMsg("操作失败");
    }

    @RequestMapping(value = "/delByIds/{ids}", method = RequestMethod.DELETE)
    @ApiOperation(value = "批量通过id删除")
    public Result<Object> delAllByIds(@PathVariable String[] ids) {

        for (String id : ids) {
            iBedService.removeById(id);
        }
        return new ResultUtil<Object>().setSuccessMsg("批量通过id删除数据成功");
    }
}
